package top.whysu.edevice.service.device.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.whysu.edevice.constant.EmqConstant;
import top.whysu.edevice.dto.app.account.AppLoginRequestDTO;
import top.whysu.edevice.dto.app.account.AppLoginResponseDTO;
import top.whysu.edevice.dto.emqx.MqttBaseRequest;
import top.whysu.edevice.dto.emqx.MqttBaseResponse;
import top.whysu.edevice.exception.BusinessException;
import top.whysu.edevice.service.app.AppService;
import top.whysu.edevice.service.device.DeviceEmqService;

import javax.annotation.Resource;

/**
 * 设备相关信息
 */
@Service("DeviceMessageServiceImpl")
public class DeviceEmqServiceImpl implements DeviceEmqService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceEmqServiceImpl.class);

    @Resource
    private AppService appService;

    @Override
    public MqttBaseResponse appLogin(MqttBaseRequest mqttBaseRequest) {
        MqttBaseResponse mqttBaseResponse = new MqttBaseResponse();
        mqttBaseResponse.setResult(EmqConstant.Code.FAIL);
        mqttBaseResponse.setParams(null);
        try {
            AppLoginRequestDTO appLoginRequestDTO = JSONObject.parseObject(JSON.toJSONString(mqttBaseRequest.getParams()), AppLoginRequestDTO.class);
            // app登录
            AppLoginResponseDTO appLoginResponseDTO = appService.wholeLogin(appLoginRequestDTO);
            mqttBaseResponse.setResult(EmqConstant.Code.SUCCESS);
            mqttBaseResponse.setParams(appLoginResponseDTO);
        } catch (BusinessException e) {
            LOGGER.error("APP登录失败", e);
            mqttBaseResponse.setResult(e.getCode());
        } catch (Exception e) {
            LOGGER.error("APP登录系统异常", e);
        }
        return mqttBaseResponse;
    }

    @Override
    public MqttBaseResponse alarmInfo(MqttBaseRequest mqttBaseRequest) {
        return null;
    }

    @Override
    public MqttBaseResponse addDevice(MqttBaseRequest mqttBaseRequest) {
        return null;
    }
}